package com.fulu.game.bi.entity;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.fulu.game.bi.common.util.lang.StringUtils;
import com.fulu.game.bi.entity.enums.QueryTypeStatusEnum;
import lombok.Data;

import javax.validation.constraints.NotNull;
import java.util.Objects;

@Data
public class BaseDTO {

    /**
     * 开始时间
     */
    private String startDate;
    /**
     * 结束时间
     */
    private String endDate;

    /**
     * 查询类型 1日 2周 3月
     */
    @NotNull(message = "queryType不能为null")
    private Integer queryType;

    /**
     * 分页页码
     */
    private Integer pageNum = 1;
    /**
     * 分页大小
     */
    private Integer pageSize = 100;


    public String getEndDate() {
        if (StringUtils.isEmpty(endDate)) {
            return endDate;
        }
        DateTime dateTime = DateUtil.parseDate(endDate);
        if (Objects.equals(getQueryType(), QueryTypeStatusEnum.WEEK.getType())) {
            return DateUtil.formatDate(DateUtil.offsetDay(dateTime, -6));
        }
        if (Objects.equals(getQueryType(), QueryTypeStatusEnum.MONTH.getType())) {
            if (dateTime.compareTo(DateUtil.beginOfDay(DateUtil.endOfMonth(dateTime))) == 0) {
                return DateUtil.format(DateUtil.beginOfMonth(dateTime), "yyyy-MM");
            }
            return DateUtil.format(DateUtil.offsetMonth(dateTime, -1), "yyyy-MM");
        }
        return endDate;
    }

    public String getStartDate() {
        if (StringUtils.isEmpty(startDate)) {
            return startDate;
        }
        DateTime dateTime = DateUtil.parseDate(startDate);
        if (Objects.equals(getQueryType(), QueryTypeStatusEnum.MONTH.getType())) {
            if (dateTime.compareTo(DateUtil.beginOfDay(DateUtil.beginOfMonth(dateTime))) == 0) {
                return DateUtil.format(DateUtil.beginOfMonth(dateTime), "yyyy-MM");
            }
            return DateUtil.format(DateUtil.offsetMonth(dateTime, 1), "yyyy-MM");
        }
        return startDate;
    }
}
